//
//  ZBNavigationBar.m
//  ZBKit
//
//  Created by xzb on 2018/8/16.
//

#import "ZBNavigationBar.h"
#import "UIColor+ZBColor.h"
#import "ZBNavigator.h"
#import "UIImage+ZBPod.h"
#import "UIView+YYAdd.h"
#import "NSObject+ZBTopViewController.h"
#import "UIColor+YYAdd.h"

@implementation ZBNavigationBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _rightMargin = 12;
        _rightItemInterval = 10;
        [self addSubview:self.titleLabel];
        [self addSubview:self.bottomLineView];
        [self addSubview:self.backBtn];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_titleLabel) {
        CGSize size = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
        _titleLabel.center = CGPointMake(self.width * 0.5, self.height * 0.5);
        _titleLabel.size = size;
    }
    if (_bottomLineView) {
        CGFloat px = 1 / [UIScreen mainScreen].scale;
        _bottomLineView.frame = CGRectMake(0, self.height - px, self.width, px);
    }
    if (_backBtn) {
        _backBtn.frame = CGRectMake(0 , 0, 44, 44);
    }
    
    if (self.rightItems && self.rightItems.count > 0) {
        __block UIView *lastView = nil;
        __weak typeof(self) weakSelf = self;
        [self.rightItems enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            CGRect tmpRect = obj.frame;
            if (!lastView) {
                tmpRect.origin.x = self.width - CGRectGetWidth(tmpRect) - strongSelf.rightMargin;
            } else {
                tmpRect.origin.x = CGRectGetMinX(lastView.frame) - CGRectGetWidth(tmpRect) - strongSelf.rightItemInterval;
            }
            tmpRect.origin.y = MAX(0, tmpRect.origin.y);
            obj.frame = tmpRect;
            lastView = obj;
        }];
    }
}

#pragma mark - api



#pragma mark - event Response
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}
- (void)backBtnClick:(UIButton *)sender
{
    [[ZBNavigator instance] popViewControllerAnimated:YES];
}

#pragma mark - private methods


#pragma mark - getter And setter
- (UIView *)bottomLineView
{
    if (!_bottomLineView){
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = [UIColor colorWithHexString:@"0xebebeb"];
    }
    return _bottomLineView;
}

- (UIButton *)backBtn
{
    if (!_backBtn){
        UIImage *image = [UIImage imageNamed:@"ic_navbar_back"];
        image = [UIImage zb_imageNamed:@"ic_navbar_back" inBundle:@"ZBKit"];
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:image forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = [UIColor colorWithHexString:@"0x333333"];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.adjustsFontSizeToFitWidth = YES;
        _titleLabel.minimumScaleFactor = 14 / [UIFont labelFontSize];
    }
    return _titleLabel;
}

- (void)setRightItems:(NSArray<UIView *> *)rightItems
{
    if (_rightItems.count > 0) {
        [_rightItems makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    _rightItems = nil;
    _rightItems = rightItems;
    
    for (UIView *subView in _rightItems) {
        [self addSubview:subView];
    }
}
@end

